//
//  colorTool.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit

class ColorTool{
    /**
        为一个字符串当中的一部分设置颜色
        start：开始着色的下标                   length：着色区间的长度
        targetText：目标文字                     color：所要上的颜色
     */
    static func setColorForPartText(start:Int,length:Int,targetText:String,color:UIColor)->NSAttributedString{
        let attributedString = NSMutableAttributedString(string: targetText)
        attributedString.addAttribute(
            .foregroundColor,
            value: color,
            range: NSRange(location: start, length: length)
        )
        return attributedString
    }
    /**
        获取一个随机颜色
     */
    static func getRandomColor()->UIColor{
        let red = CGFloat.random(in: 0...1)
        let green = CGFloat.random(in: 0...1)
        let blue = CGFloat.random(in: 0...1)
        return UIColor(red:red,green:green,blue:blue,alpha: 1.0)
    }
    /**
        获取一个渐变色
        c：要渐变的颜色
     */
    static func getGradient(c:UIColor)->UIColor {
        let gradientColor = UIColor { traitCollection in
            let colors = traitCollection.userInterfaceStyle == .dark ?
                [UIColor.black, UIColor.purple] :
                [UIColor.white, c]
            
            let gradient = CAGradientLayer()
            gradient.colors = colors.map { $0.cgColor }
            gradient.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            
            UIGraphicsBeginImageContext(gradient.bounds.size)
            gradient.render(in: UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            return UIColor(patternImage: image!)
        }
        return gradientColor
    }
    /**
        获取一个渐变色
        from：初始渐变的颜色
        to：最终渐变的颜色
     */
    static func getGradientFromTo(from:UIColor,to:UIColor)->UIColor {
        let size = CGSize(width: UIScreen.main.bounds.width, height: 2) // 使用屏幕宽度
        let gradient = CAGradientLayer()
        gradient.frame = CGRect(origin: .zero, size: size)
        gradient.colors = [from.cgColor, to.cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 0.5)
        gradient.endPoint = CGPoint(x: 1, y: 0.5)
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        defer { UIGraphicsEndImageContext() }
        
        gradient.render(in: UIGraphicsGetCurrentContext()!)
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
            return from // 失败时返回起始颜色
        }
        
        return UIColor(patternImage: image)
    }
    static func applyGradient(to view: UIView, from: UIColor, to: UIColor) {
        let gradient = CAGradientLayer()
        gradient.colors = [from.cgColor, to.cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 0.5)
        gradient.endPoint = CGPoint(x: 1, y: 0.5)
        gradient.frame = view.bounds
        
        // 移除已有的渐变层
        view.layer.sublayers?
            .filter { $0 is CAGradientLayer }
            .forEach { $0.removeFromSuperlayer() }
        
        view.layer.insertSublayer(gradient, at: 0)
    }
}
